package com.yongcheon.sms_manager.receiver.sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.SmsMessage;

import com.yongcheon.sms_manager.data.preference.SpamNumberData;
import com.yongcheon.sms_manager.data.preference.SpamSentenceData;
import com.yongcheon.sms_manager.data.sms_spam.SMS_Spam_Data;
import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.notification.NormalNotification;
import com.yongcheon.sms_manager.notification.SpamNotification;
import com.yongcheon.sms_manager.notification.SpamSentenceNotification;
import com.yongcheon.sms_manager.utils.AndroidSystemUtil;
import com.yongcheon.sms_manager.utils.DefaultAppUtil;
import com.yongcheon.sms_manager.utils.StringUtil;
import com.yongcheon.sms_manager.vo.SMSVO;
import com.yongcheon.sms_manager.vo.SpamNumVO;
import com.yongcheon.sms_manager.vo.SpamSentenceVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-11-28.
 */
public class SmsReceiver extends BroadcastReceiver {
    public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    public static final int NOTI_ID = 1;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(SMS_RECEIVED)) {
            Bundle bundle = intent.getExtras();
            if (bundle == null) return;

            Object[] pdusObj = (Object[]) bundle.get("pdus");
            if (pdusObj == null) return;
            SMSVO smsVO = getSMSVOFormMessage(pdusObj);

            if(DefaultAppUtil.checkIsDefault(context)) {
                if(AndroidSystemUtil.ANDROID_VERSION > 16) {
                    storeMsg(intent, context, pdusObj);
                    NormalNotification normalNotification = new NormalNotification();
                    normalNotification.setNotification(context, smsVO);
                }
            }

            if(DefaultAppUtil.isEnbleSMSBlick(context)) {
                SpamNumVO spamVO = getSpamVo(smsVO.getAddress(), context);
                if (spamVO != null) {
                    SpamNotification spamNotification = new SpamNotification();
                    spamNotification.setNotification(context, spamVO, smsVO);
                } else {
                    SpamSentenceVO spamSentenceVO = getSpamSentenceVo(smsVO, context);
                    if (spamSentenceVO != null) {
                        SpamSentenceNotification spamSentenceNotification = new SpamSentenceNotification();
                        spamSentenceNotification.setNotification(context, spamSentenceVO, smsVO);
                    }
                }
            }
        }
    }

    public SMSVO getSMSVOFormMessage(Object[] pdusObj){
        SMSVO vo = new SMSVO();
        SmsMessage[] smsMessages = new SmsMessage[pdusObj.length];
        String messageBody = "";
        String address = "";
        long time = 0;
        for (int i = 0; i < pdusObj.length; i++) {
            smsMessages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
            messageBody += smsMessages[i].getMessageBody();
            if(i == 0) {
                address = smsMessages[i].getOriginatingAddress();
                time = smsMessages[i].getTimestampMillis();
            }
        }
        vo.setAddress(address);
        vo.setBody(messageBody);
        vo.setDate(time);
        return vo;
    }

    public SpamNumVO getSpamVo(String address, Context context){
        SQLiteDatabase database = SpamDBManager.getDatabase(context);
        SpamNumVO spamVo = null;
        SpamNumberData spamNumberData = new SpamNumberData(context);
        ArrayList<SpamNumVO> list =  spamNumberData.selectReturnVo(address, context, database);
        if(list.size()> 0) spamVo = list.get(0);
        return spamVo;
    }

    public void storeMsg(Intent intent,Context context, Object[] pdusObj){
        int error = intent.getIntExtra("errorCode", 0);
        SMS_Spam_Data.storeMsg(context, pdusObj, error);
    }

    public SpamSentenceVO getSpamSentenceVo(SMSVO smsVOy, Context context){
        SpamSentenceVO spamSentenceVO = null;
        String onlyLetterBody = StringUtil.getOnlyLetter(smsVOy.getBody());
        SpamSentenceData spamSentenceData = new SpamSentenceData(context);
        ArrayList<SpamSentenceVO> list = spamSentenceData.selectReturnVo(null, context, SpamDBManager.getDatabase(context));
        for(int i=0; i<list.size(); i++){
            SpamSentenceVO tmepSpamSentenceVO = list.get(i);
            if(onlyLetterBody.contains(tmepSpamSentenceVO.getSentence())){
                spamSentenceVO = tmepSpamSentenceVO;
                break;
            }
        }
        return spamSentenceVO;
    }
}
